package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.programming.common.Result;
import com.example.programming.common.tools.BaseController;
import com.example.programming.sys.entity.Chapter;
import com.example.programming.sys.entity.Homework;
import com.example.programming.sys.mapper.ChapterMapper;
import com.example.programming.sys.mapper.HomeworkMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 章节 前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-06-30
 */
@MapperScan("com.example.programming")
@RestController
@RequestMapping("/sys/chapter")
public class ChapterController extends BaseController {
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private HomeworkMapper homeworkMapper;
    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Chapter> page = new Page<Chapter>(pageNum, pageSize);
        Chapter parms = new Chapter();
        QueryWrapper<Chapter> warpper = new QueryWrapper<Chapter>(parms);

        result.setData(chapterMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Chapter entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            chapterMapper.insert(entity);
        } else {
            chapterMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        chapterMapper.deleteBatchIds(deleteIds);
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/getch")
    public Result getch(String course, String teacher) {
        Result result = new Result();
        Map map = new HashMap();
        map.put("course", course);
        map.put("teacher", teacher);
        result.setData(chapterMapper.selectByMap(map));
        return result;
    }
    @RequestMapping(method = RequestMethod.POST, value = "/gettitle")
    public Result gettitle(String url) {
        Result result = new Result();
        Chapter parms = new Chapter();
        QueryWrapper<Chapter> warpper = new QueryWrapper<Chapter>(parms);
        warpper.eq("url", url);
        result.setData(chapterMapper.selectOne(warpper));
        return result;
    }

    /**
     * 上传章节视频
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/videoUpload/{id}")
    public R upload(@RequestParam("file") MultipartFile file,@PathVariable("id")String id) throws IOException {
        String fileId = UUID.randomUUID().toString().replace("-","");
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        String url = "D://video/"+fileId+prefix;
        File file1 = new File(url);
        File file2 = new File("D://video/");
        if(!file2.exists()){//如果文件夹不存在
            file2.mkdir();//创建文件夹
        }
        file.transferTo(file1);
        Chapter chapter = chapterMapper.selectById(id);
        chapter.setUrl(url);
        chapter.setVideoName(fileName);
        chapterMapper.updateById(chapter);
        return R.ok("上传成功");
    }

    /**
     * 上传章节作业
     * @param file
     * @param id
     * @return
     * @throws IOException
     */
    @PostMapping("/homeworkUpload/{id}")
    public R homewordUpload(@RequestParam("file") MultipartFile file,@PathVariable("id")String id) throws IOException {
        String fileId = UUID.randomUUID().toString().replace("-","");
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        String url = "D://homework/"+fileId+prefix;
        File file1 = new File(url);
        File file2 = new File("D://homework/");
        if(!file2.exists()){//如果文件夹不存在
            file2.mkdir();//创建文件夹
        }
        file.transferTo(file1);
        Chapter chapter = chapterMapper.selectById(id);
        Homework homework = homeworkMapper.selectOne(new QueryWrapper<Homework>().eq("chapter_id",id));
        if(homework == null){
            Homework homework1 = new Homework();
            homework1.setChapterId(id);
            homework1.setUrl(url);
            homework1.setName(chapter.getChapterName());
            homework1.setCourse(chapter.getCourse());
            homework1.setHomeworkName(fileName);
            homework1.setTeacher(chapter.getTeacher());
            homeworkMapper.insert(homework1);
        }else{
            homework.setUrl(url);
            homework.setHomeworkName(fileName);
            homeworkMapper.updateById(homework);
        }
        return R.ok("上传成功");
    }
}
